
import { getProductDetail, getProductSku } from '../../services/goods';
import parser from 'rich-text-parser'
import { dateAddDays3 } from '../../utils/date';
Page({
  data: {
    unitPrice: 0,
    specImage: '',
    selectSpecTitile: '请选择颜色 规格 租用 天数',
    nodes: [],
    globalData: getApp().globalData,
    tabIndex: 1,
    goodsInfo: {},
    productId: '',
    showtitle: '',
    modalOpened: false,
    colorActiveId: -1,
    specActiveId: -1,
    rentActiveId: -1,
    dayActiveId: -1,
    colorList: [],
    specList: [],
    rentDayList: [],
    dayList: [],
    skuInfo: {},
    productSkuInfo: {},
    showBottom: false,
    tagTitle: ''
  },
  onShow() {
    my.setNavigationBar({
      title: '商品详情',
      backgroundColor: '#4BA3F9'
    })
  },
  onModalClick() {
    this.setData({
      modalOpened: false,
    });
  },
  onModalClose() {
    this.setData({
      modalOpened: false,
    });
  },
  priceDetail() {
    this.setData({
      modalOpened: true,
    });
  },
  tabClick(event) {

    this.setData({ tabIndex: event.target.dataset.num });
  },
  onClickSku(event) {
    console.log(event)
    if (parseInt(event.target.dataset.type) === 1)//颜色
    {

      if (this.data.colorActiveId == event.target.dataset.index) {//再次点击选中的，取消选中
        this.setData({ colorActiveId: -1 });
        let specList = this.data.specList
        specList.forEach(b => {
          b.isdisable = false
        });
        let colorist = this.data.colorList
        colorist.forEach(b => {
          b.isdisable = false
        });
        this.setData({ colorList: colorist })
        this.setData({ specList: specList })
        this.oncalcSku2()
        return
      }
      if (this.data.colorList[event.target.dataset.index].isdisable) {//禁止的不执行
        return
      }
      this.setData({ colorActiveId: event.target.dataset.index });
      let datalist = this.data.specList
      datalist.forEach(b => {
        b.isdisable = true
      });
      this.data.skuInfo.productSkuList.forEach(a => {
        if (a.productSkuSpec.colorName == this.data.colorList[event.target.dataset.index].name) {
          datalist.forEach(b => {
            if (b.name == a.productSkuSpec.specName) {
              b.isdisable = false
            }
          });
        }
      })
      this.setData({ specList: datalist })
      this.oncalcSku2()
    } else if (parseInt(event.target.dataset.type) == 2) {
      if (this.data.specActiveId == event.target.dataset.index) {//再次点击选中的，取消选中
        this.setData({ specActiveId: -1 });
        let datalist = this.data.colorList
        datalist.forEach(b => {
          b.isdisable = false
        });
        let specList = this.data.specList
        specList.forEach(b => {
          b.isdisable = false
        });
        this.setData({ specList: specList })
        this.setData({ colorList: datalist })
        this.oncalcSku2()
        return
      }
      if (this.data.specList[event.target.dataset.index].isdisable) {
        return
      }
      this.setData({ specActiveId: event.target.dataset.index });
      let datalist = this.data.colorList
      datalist.forEach(b => {
        b.isdisable = true
      });
      this.data.skuInfo.productSkuList.forEach(a => {
        if (a.productSkuSpec.specName == this.data.specList[event.target.dataset.index].name) {
          datalist.forEach(b => {
            if (b.name == a.productSkuSpec.colorName) {
              b.isdisable = false
            }
          });
        }
      })
      this.setData({ colorList: datalist })
      this.oncalcSku2()
    }

  },
  changerentday(event) {

    this.setData({ rentActiveId: event.target.dataset.index });
  },
  changeday(event) {
    this.setData({ dayActiveId: event.target.dataset.index });
    this.oncalcSku2()
  },
  settagTitle(oldNewDegree) {
    let title = ''
    switch (oldNewDegree) {
      case 1:
        title = '全新'
        break;
      case 2:
        title = '99新'
        break;
      case 3:
        title = '95新'
        break;
      case 4:
        title = '9成新'
        break;
      case 5:
        title = '8成新'
        break;
      case 6:
        title = '7成新'
        break;
      default:
        break;
    }
    this.setData({ tagTitle: title });
  },
  oncalcSku2() {
    let specImage = this.data.goodsInfo.mainImage
    let unitPrice = this.data.goodsInfo.cheapUnitPrice
    let selectSpecTitile = '请选择颜色 规格 租用天数'
    let that = this
    if (this.data.colorActiveId >= 0 && this.data.specActiveId >= 0 && this.data.dayActiveId >= 0) {//全部选择
      this.data.skuInfo.productSkuList.forEach(element => {
        if (element.productSkuSpec.colorName == that.data.colorList[that.data.colorActiveId].name && element.productSkuSpec.specName == that.data.specList[that.data.specActiveId].name) {
          specImage = element.skuImage;
          unitPrice = this.data.dayList[this.data.dayActiveId].unitPrice;
          let productSkuInfo = element.productSkuRentPriceList[this.data.dayActiveId]
          productSkuInfo.totalInventory = element.totalInventory
          this.setData({ productSkuInfo: element.productSkuRentPriceList[this.data.dayActiveId] });
          this.settagTitle(element.productSkuSpec.oldNewDegree);
          selectSpecTitile = '已选择' + element.productSkuSpec.colorName + ' ' + element.productSkuSpec.specName + ' ' + this.data.dayList[this.data.dayActiveId].title
        }
      });
    }
    else if (this.data.colorActiveId >= 0 && this.data.specActiveId >= 0 && this.data.dayActiveId < 0) {//颜色 规格 选择
      this.data.skuInfo.productSkuList.forEach(element => {
        this.setData({ productSkuInfo: {} });
        if (element.productSkuSpec.colorName == this.data.colorList[that.data.colorActiveId].name && element.productSkuSpec.specName == this.data.specList[this.data.specActiveId].name) {
          specImage = element.skuImage;
          this.settagTitle(element.productSkuSpec.oldNewDegree);
          element.productSkuRentPriceList.forEach(a => {
            a.title = a.rentDay + '天'
          });
          console.log(element.productSkuRentPriceList)
          this.setData({ dayList: element.productSkuRentPriceList });
          this.setData({ dayActiveId: -1 });
        }
      });
      selectSpecTitile = '请选择租用天数'
    }
    else if (this.data.colorActiveId >= 0 && this.data.specActiveId < 0) {//颜色选择 规格未选择
      this.setData({ dayList: [] });
      this.setData({ dayActiveId: -1 });
      selectSpecTitile = '请选择规格 租用天数'
    }
    else if (this.data.colorActiveId < 0 && this.data.specActiveId >= 0) {//颜色未选择 规格选择
      this.setData({ dayList: [] });
      this.setData({ dayActiveId: -1 });
      selectSpecTitile = '请选择颜色 租用天数'
    }
    else if (this.data.colorActiveId < 0 && this.data.specActiveId < 0) {//颜色未选择 规格未选择
      this.setData({ dayList: [] });
      this.setData({ dayActiveId: -1 });
    }
    this.setData({ specImage: specImage });
    this.setData({ unitPrice: unitPrice });
    this.setData({ selectSpecTitile: selectSpecTitile });
  },

  onPopupClose() {
    this.setData({
      showBottom: false
    });
  },
  onPopupSure() {
    if (this.data.colorActiveId < 0) {
      my.showToast({
        type: 'fail',
        content: '请选择颜色',
        duration: 2000,
      });
      return
    }
    if (this.data.specActiveId < 0) {
      my.showToast({
        type: 'fail',
        content: '请选择规格',
        duration: 2000,
      });
      return
    }
    if (this.data.dayActiveId < 0) {
      my.showToast({
        type: 'fail',
        content: '请选择租用天数',
        duration: 2000,
      });
      return
    }
    if (this.data.rentActiveId < 0) {
      my.showToast({
        type: 'fail',
        content: '请选择起租日期',
        duration: 2000,
      });
      return
    }
    if (this.data.productSkuInfo.totalInventory <= 0) {
      my.showToast({
        type: 'fail',
        content: '库存不足',
        duration: 2000,
      });
      return
    }
    let pushdata = 'skuId=' + this.data.productSkuInfo.skuId + '&rentDay=' + this.data.dayList[this.data.dayActiveId].rentDay + '&rentTime=' + this.data.rentDayList[this.data.rentActiveId].value;
    pushdata += '&skuImage=' + this.data.specImage;
    pushdata += '&specName=' + this.data.selectSpecTitile;
    pushdata += '&goodsName=' + this.data.goodsInfo.productName

    my.navigateTo({
      url: '/pages/orderSure/orderSure?' + pushdata
    })
  },
  onPopupOpen() {
    if (Object.keys(this.data.skuInfo).length === 0) {
      this.getProductSku()
    } else {
      this.setData({
        showBottom: true,
      });
    }

  },
  goHome() {
    my.switchTab({
      url: '/pages/home/home',
    })
  },
  goCustomer() {
    my.switchTab({
      url: '/pages/customer/customer',
    })
  },
  onLoad(event) {
    console.log(event)
    this.setData({ productId: event.id })
    // this.mySchedulde();
    this.getProductDetail()

  },
  getProductDetail() {
    let param = {
      productId: this.data.productId
    }
    getProductDetail(param).then((res) => {
      if (res.code == "000000") {
        this.setData({ goodsInfo: res.data })

        if (res.data.productDetail != undefined && res.data.productDetail.length > 0)
          parser.definedCustomTag({ figure: 'div', figcaption: '' })
        const nodes = parser.getRichTextJson(res.data.productDetail)
        this.setData({
          nodes: nodes.children
        });
        let title = ''
        if (res.data.isRenewal) {
          title += '可租';
        }
        if (res.data.isPurchase) {
          title += '可买断';
        }
        this.setData({
          showtitle: title
        });
        this.oncalcSku2()
      }

    })

  },
  async getProductSku() {
    let param = {
      productId: this.data.productId
    }
    await getProductSku(param).then((res) => {
      console.log(res)
      if (res.code == "000000") {
        let colorList = [];
        res.data.colorNameSet.split(",").forEach(a => {
          let colorInfo = {}
          colorInfo.name = a
          colorInfo.isdisable = false
          colorList.push(colorInfo)
        });
        let specList = [];
        res.data.specNameSet.split(",").forEach(a => {
          let specInfo = {}
          specInfo.name = a
          specInfo.isdisable = false
          specList.push(specInfo)
        });
        this.setData({
          skuInfo: res.data,
          colorList: colorList,
          specList: specList,
          showBottom: true,
        });
        //this.oncalcSku()
        var pdate = dateAddDays3(new Date(), this.data.globalData.playforminfo.startRentDay);
        let datalist = []
        for (let index = 0; index < 7; index++) {
          let datainfo = {}
          let date = dateAddDays3(pdate, index)

          datainfo.title = (date.getMonth() + 1).toString().padStart(2, '0') + "月" + date.getDate() + "号"
          datainfo.value = date.getFullYear() + "-" + (date.getMonth() + 1).toString().padStart(2, '0') + "-" + date.getDate().toString().padStart(2, '0');
          datalist.push(datainfo)
        }
        this.setData({
          rentDayList: datalist
        });
      }
    })

  },
});
